define(['jquery', 'bootstrap', 'backend', 'table', 'form',  'upload'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'pay/index',
                    add_url: 'pay/add',
                    edit_url: 'pay/edit',
                    del_url: 'pay/del',
                    multi_url: 'pay/multi',
                    dragsort_url: 'ajax/weigh',
                    table: 'payment',
                },
            });


            // start_time
            // end_time
            // currency
            // money
            // valid_time
            // order_status
            // pay_status
            // print_status
            // create_time


            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pagination: true,
                commonSearch: true,
                search: true,
                fixedColumns: true,         // 打开冻结按钮
                fixedNumber:  2,            // 固定左侧，固定左侧几列填几
                fixedRightNumber: 4,        // 固定右侧，固定右侧几列填几
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID'), sortable: true},
                        {field: 'order_no', title: __('OrderNo'), sortable: true, formatter: Controller.api.formatter.filename, operate: 'like'},
                        {field: 'username', title: __('UserName'), sortable: true, formatter: Controller.api.formatter.filename, operate: 'like'},
                        {field: 'phone', title: __('Phone'), sortable: true, formatter: Controller.api.formatter.filename, operate: 'like', visible: false},
                        {field: 'buddha_name', title: __('BuddhaName'), sortable: true, formatter: Controller.api.formatter.buddha_name, operate: 'like'},
                        // {field: 'buddha_no', title: __('BuddhaNo'), sortable: true, formatter: Controller.api.formatter.filename, operate: 'like'},
                        {field: 'blessing', title: __('BlessingText'), sortable: true, formatter: Controller.api.formatter.filename, operate: 'like', visible: false},
                        {field: 'timespan_title', title: __('Timespan'), sortable: true, formatter: Controller.api.formatter.filename, operate: 'like'},
                        {field: 'currency', title: __('Currency'), sortable: true, formatter: Controller.api.formatter.filename, operate: 'like', visible: false},
                        {field: 'money', title: __('TotalMoney'), sortable: true, formatter: Controller.api.formatter.filename, operate: 'like'},
                        {field: 'start_time', title: __('StartTime'), sortable: true, formatter: Table.api.formatter.datetime, datetimeFormat: "YYYY-MM-DD"},                        
                        {field: 'end_time', title: __('EndTime'), sortable: true, formatter: Table.api.formatter.datetime, datetimeFormat: "YYYY-MM-DD"},                                            
                        {
                            field: 'create_time',
                            title: __('Createtime'),
                            sortable: true,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },                                               
                        // 订单状态
                        {field: 'order_status', title: __('OrderStatus'),sortable: true,  operate: false, formatter:function (value, row, index) {
                                if(value == 0){
                                    return '<span class="text-success label label-danger">' + __('未确认') + '</span>';
                                }else if(value == 1){
                                    return '<span class="text-success label label-success">' + __('已确认') + '</span>';
                                }else{
                                    return '<span class="text-success label label-warning">' + __('已取消') + '</span>';
                                }
                            }
                        },   
                        // 支付状态
                        {field: 'pay_status', title: __('PayStatus'),sortable: true,  operate: false, formatter:function (value, row, index) {
                                if(value == 0){
                                    return '<span class="text-success label label-danger">' + __('未支付') + '</span>';
                                }else if(value == 1){
                                    return '<span class="text-success label label-success">' + __('已支付') + '</span>';
                                }
                            }
                        }, 
                        // 打印状态
                        {field: 'print_status', title: __('PrintStatus'),sortable: true,  operate: false, formatter:function (value, row, index) {
                                if(value == 0){
                                    return '<span class="text-success label label-danger">' + __('未打印') + '</span>';
                                }else if(value == 1){
                                    return '<span class="text-success label label-success">' + __('已打印') + '</span>';
                                }
                            }
                        },  
                        // 预览效果
                        // {field: 'view_url', title: __('View'), align: 'center', formatter:function (value, row, index) {
                        //         var html = `<div class="input-group input-group-sm">
                        //                     <span class="input-group-btn input-group-sm">
                        //                         <a href="/` + Config.frontend_module + `/ebook/download/id/${row.id}.html" target="_blank" class="btn btn-default btn-sm">
                        //                             <i class="fa fa-link"></i>${__('Preview')}
                        //                         </a>
                        //                     </span>
                        //                 </div>`;
                        //         return html;
                        //     }
                        // },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'edit',
                                    text: __('Edit'),
                                    title: __('Edit'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-edit',
                                    extend: 'data-area=\'["90%","95%"]\'',
                                    url: 'pay/edit',
                                    callback: function(data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), { title: "回传数据" });
                                    },
                                    visible: function(row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },
                                {
                                    name: 'click',
                                    title: __('点击执行事件'),
                                    classname: 'btn btn-xs btn-info btn-click',
                                    icon: 'fa fa-leaf',
                                    // dropdown: '更多',//如果包含dropdown，将会以下拉列表的形式展示
                                    click: function (data) {
                                        Layer.alert("点击按钮执行的事件");
                                    }
                                },
                                {
                                    name: 'detail',
                                    title: __('弹出窗口打开'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'example/bootstraptable/detail',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    }
                                },
                                {
                                    name: 'ajax',
                                    title: __('发送Ajax'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-magic',
                                    confirm: '确认发送Ajax请求？',
                                    url: 'order/paystatus',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'addtabs',
                                    title: __('新选项卡中打开'),
                                    classname: 'btn btn-xs btn-warning btn-addtabs',
                                    icon: 'fa fa-folder-o',
                                    url: 'example/bootstraptable/detail'
                                }
                            ]
                        }
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);

            
            // 假设你已经初始化了BootstrapTable，并且你的表格ID为#table
            $("#table").on("click", ".btn-editone", function(){
                // 获取当前行数据
                var row = $(this).closest('tr[data-index]');
                var rowData = $("#table").bootstrapTable('getData')[row.data('index')];
                window.location.href = '/goods/edit/' + rowData.id;
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on("change", "#c-type", function () {
                    $("#c-pid option[data-type='all']").prop("selected", true);
                    $("#c-pid option").removeClass("hide");
                    $("#c-pid option[data-type!='" + $(this).val() + "'][data-type!='all']").addClass("hide");
                    $("#c-pid").data("selectpicker") && $("#c-pid").selectpicker("refresh");
                });
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                // thumb: function (value, row, index) {
                //     var html = '';
                //     if (row.mimetype.indexOf("image") > -1) {
                //         html = '<a href="' + row.fullurl + '" target="_blank"><img src="' + row.fullurl + row.thumb_style + '" alt="" style="max-height:60px;max-width:120px"></a>';
                //     } else {
                //         html = '<a href="' + row.fullurl + '" target="_blank"><img src="' + Fast.api.fixurl("ajax/icon") + "?suffix=" + row.imagetype + '" alt="" style="max-height:90px;max-width:120px"></a>';
                //     }
                //     return '<div style="width:120px;margin:0 auto;text-align:center;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;">' + html + '</div>';
                // },
                // url: function (value, row, index) {
                //     return '<a href="' + row.fullurl + '" target="_blank" class="label bg-green">' + row.url + '</a>';
                // },
                filename: function (value, row, index) {
                    return '<div style="width:150px;margin:0 auto;text-align:center;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;">' + Table.api.formatter.search.call(this, value, row, index) + '</div>';
                },
                buddha_name: function (value, row, index) {
                    return '<div style="width:150px;margin:0 auto;text-align:center;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;">' + row.buddha_name + " " + row.buddha_no + '号佛灯</div>';
                },
                // mimetype: function (value, row, index) {
                //     return '<div style="width:80px;margin:0 auto;text-align:center;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;">' + Table.api.formatter.search.call(this, value, row, index) + '</div>';
                // },
            }
        }
    };
    return Controller;
});
